package com.hs.telepass.ui.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import butterknife.ButterKnife;
import butterknife.InjectView;
import com.hs.teleform.R;
import com.hs.telepass.Constants;
import com.hs.telepass.model.Profile;
import com.squareup.picasso.Picasso;

import java.util.List;

public class ProfileAdapter extends BaseAdapter{

    private Context mContext;
    private List<Profile> mProfilesList;

    public ProfileAdapter(Context context, List<Profile> profilesList){
        this.mContext = context;
        this.mProfilesList = profilesList;
    }

    @Override
    public int getCount() {
        return mProfilesList.size();
    }

    @Override
    public Object getItem(int position) {
        return mProfilesList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return -1;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView != null) {
            holder = (ViewHolder) convertView.getTag();
        } else {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.history_item, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }

        Profile profile = mProfilesList.get(position);
        holder.txtViewHost.setText(profile.getTitle());
        holder.txtViewUserName.setText(profile.getUsername());

        if(Constants.SOURCE_TELEFORM == profile.getSource()){
            Picasso.with(mContext).load(R.drawable.telepass_placeholder).into(holder.imgViewLogo);
        }else if(Constants.SOURCE_SINGOU == profile.getSource()){
            Picasso.with(mContext).load(profile.getImgPath()).into(holder.imgViewLogo);
        }

        return convertView;
    }

    static class  ViewHolder{
        @InjectView(R.id.logo)
        ImageView imgViewLogo;
        @InjectView(R.id.txtview_host)
        TextView txtViewHost;
        @InjectView(R.id.txtview_username)
        TextView txtViewUserName;

        public ViewHolder(View view){
            ButterKnife.inject(this, view);
        }
    }
}
